package com.traffic.between.util;

import java.io.File;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;

public class ToolUtils {

	public static String CMCC = "CMCC";
	public static String UNICOM = "UNICOM";
	public static String TELECOM = "TELECOM";
	public static String UNKNWON = "UNKNWON";

	public static String decimalFormatToString(double doubleValue, String format) {
		DecimalFormat myFormatter = new DecimalFormat(format);
		String formatValue = myFormatter.format(doubleValue);
		return formatValue;
	}

	public static String getUuidByJdk(boolean is32bit) {
		String uuid = UUID.randomUUID().toString();
		if (is32bit) {
			return uuid.toString().replace("-", "");
		}
		return uuid;
	}

	public static String getOrderId(int lg) {
		String uuid = getUuidByJdk(true);
		return "F" + uuid.substring(0, lg);
	}

	public static String getOrderId(int lg, String prefix) {
		String uuid = getUuidByJdk(true);
		return prefix + uuid.substring(0, lg);
	}

	public static boolean isBlank(Object obj) {
		if (obj == null)
			return true;
		if (obj instanceof String)
			return StringUtils.isBlank((String) obj);
		if (obj instanceof Map)
			return ((Map) obj).isEmpty();
		if (obj instanceof List) {
			return ((List) obj).isEmpty();
		}
		return false;
	}

	public static String getNet(String phoneNum) {
		String head3 = "";
		String head4 = "";
		phoneNum = phoneNum.trim();

		if ((phoneNum == null) || (phoneNum.length() < 11)) {
			return UNKNWON;
		}
		if (phoneNum.startsWith("+")) {
			phoneNum = phoneNum.substring(1);
		}
		if (phoneNum.startsWith("86")) {
			phoneNum = phoneNum.substring(2);
		}

		if (phoneNum.length() != 11) {
			return UNKNWON;
		}

		head3 = phoneNum.substring(0, 3);
		head4 = phoneNum.substring(0, 4);

		if ((head3.equals("135")) || (head3.equals("136")) || (head3.equals("137")) || (head3.equals("138"))
				|| (head3.equals("139")) || (head3.equals("150")) || (head3.equals("151")) || (head3.equals("152"))
				|| (head3.equals("157")) || (head3.equals("158")) || (head3.equals("159")) || (head3.equals("182"))
				|| (head3.equals("183")) || (head3.equals("184")) || (head3.equals("187")) || (head3.equals("188"))
				|| (head3.equals("147"))) {
			return CMCC;
		}
		if ((head4.equals("1340")) || (head4.equals("1341")) || (head4.equals("1342")) || (head4.equals("1343"))
				|| (head4.equals("1344")) || (head4.equals("1345")) || (head4.equals("1346")) || (head4.equals("1347"))
				|| (head4.equals("1348"))) {
			return CMCC;
		}
		if ((head3.equals("130")) || (head3.equals("131")) || (head3.equals("132")) || (head3.equals("145"))
				|| (head3.equals("155")) || (head3.equals("156")) || (head3.equals("175")) || (head3.equals("176"))
				|| (head3.equals("185")) || (head3.equals("186"))) {
			return UNICOM;
		}
		if ((head3.equals("133")) || (head3.equals("153")) || (head3.equals("180")) || (head3.equals("181"))
				|| (head3.equals("189"))) {
			return TELECOM;
		}
		if (head4.equals("1349")) {
			return TELECOM;
		}

		return UNKNWON;
	}

	public static String renameTo(File file) {
		String fileName = getUuidByJdk(true) + "." + file.getName().substring(file.getName().lastIndexOf(".") + 1);
		file.renameTo(new File(file.getParent() + "/" + fileName));
		return file.getParent() + "/" + fileName;
	}
}